package com.ychug.gateway.cms;

import java.util.ArrayList;
import java.util.List;

import com.ychug.common.utils.DateUtils;
import com.ychug.framework.business.dto.CmsNewsDTO;
import com.ychug.framework.business.service.SystemSerialNumber;
import com.ychug.service.cms.entity.CmsNewsDataEntity;
import com.ychug.service.cms.entity.CmsNewsEntity;
import com.ychug.service.cms.service.ICmsNewsDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ychug.service.cms.service.ICmsNewsService;
import com.ychug.framework.web.base.BaseController;
import com.ychug.framework.web.page.TableDataInfo;
import com.ychug.common.base.ResultAjaxHash;

/**
 * 内容 信息操作处理
 * 
 * @author mcpcms
 * @date 2019-04-30
 */
@Controller
@RequestMapping("/cms/cmsNews")
public class CmsNewsController extends BaseController
{
    private String prefix = "modules/cms/news";
	
	@Autowired
	private ICmsNewsService cmsNewsService;

	@Autowired
	private ICmsNewsDataService cmsNewsDataService;

	@Autowired
	private SystemSerialNumber systemSerialNumber;

	@GetMapping()
	public String cmsNews()
	{
	    return prefix + "/viewNews";
	}
	
	/**
	 * 查询内容列表
	 */
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(CmsNewsDTO cmsNewsDTOData)
	{
		startPage();

		List<CmsNewsDTO> list = new ArrayList<>();
		CmsNewsEntity cmsNewsData = new CmsNewsEntity();

//		cmsNewsData.setStatus(Integer.parseInt(cmsNewsDTOData.getStatus()));

		//查询文档主表中的数据
        List<CmsNewsEntity> cmsNewsEntityList = cmsNewsService.selectCmsNewsList(cmsNewsData);
        for(CmsNewsEntity cmsNews : cmsNewsEntityList){

			//查询从表中的数据
			CmsNewsDataEntity cmsNewsDataEntity = new CmsNewsDataEntity();
			cmsNewsDataEntity.setContentId(Integer.parseInt(cmsNews.getContentCode()));
			List cmsNewsDataEntityList =  cmsNewsDataService.selectCmsNewsDataList(cmsNewsDataEntity);
			cmsNewsDataEntity = (CmsNewsDataEntity) cmsNewsDataEntityList.get(0);

			CmsNewsDTO cmsNewsDTO = new CmsNewsDTO();
			cmsNewsDTO.setContentId(cmsNews.getContentId());
			cmsNewsDTO.setSiteId(cmsNews.getSiteId());
			cmsNewsDTO.setContentCode(cmsNews.getContentCode());
			cmsNewsDTO.setCatId(cmsNews.getCatId());
			cmsNewsDTO.setCatName(cmsNews.getCatName());

			cmsNewsDTO.setPublishDate(DateUtils.parseDateToStr("yyyy-MM-dd",cmsNews.getPublishDate()));
			cmsNewsDTO.setOffDate(DateUtils.parseDateToStr("yyyy-MM-dd",cmsNews.getOffDate()));
			cmsNewsDTO.setPriority(cmsNews.getPriority());
			cmsNewsDTO.setStatus(String.valueOf(cmsNews.getStatus()));
			cmsNewsDTO.setCheckStatus(cmsNews.getCheckStatus());

			cmsNewsDTO.setTitle("["+cmsNews.getCatName()+"] "+cmsNewsDataEntity.getTitle());
			cmsNewsDTO.setFullTitle(cmsNewsDataEntity.getFullTitle());
			cmsNewsDTO.setLink(cmsNewsDataEntity.getLink());
			cmsNewsDTO.setIsNewWindow(cmsNewsDataEntity.getIsNewWindow());
			cmsNewsDTO.setColor(cmsNewsDataEntity.getColor());
			cmsNewsDTO.setNewsPath(cmsNewsDataEntity.getNewsPath());
			cmsNewsDTO.setNewsTemplate(cmsNewsDataEntity.getNewsTemplate());
			cmsNewsDTO.setDescription(cmsNewsDataEntity.getDescription());
			cmsNewsDTO.setSource(cmsNewsDataEntity.getSource());
			cmsNewsDTO.setSourceUrl(cmsNewsDataEntity.getSourceUrl());
			cmsNewsDTO.setContent(cmsNewsDataEntity.getContent());
			cmsNewsDTO.setAuthor(cmsNewsDataEntity.getAuthor());
			list.add(cmsNewsDTO);
		}
		return getDataTable(list);
	}
	
	/**
	 * 新增内容
	 */
	@GetMapping("/add")
	public String add()
	{
//		String ContentNo = systemSerialNumber.createSerialNumber("ContentNo","CMS");
//		System.out.println(ContentNo);
		return prefix + "/addNews";
	}
	
	/**
	 * 新增保存内容
	 */
	@PostMapping("/add")
	@ResponseBody
	public ResultAjaxHash addSave(CmsNewsDTO cmsNewsDTO)
	{
		cmsNewsDTO.setCreateBy(getLoginName());
		String ContentNo = systemSerialNumber.createSerialNumber("ContentNo","CMS");
		cmsNewsDTO.setContentCode(ContentNo);

		CmsNewsEntity cmsNews = new CmsNewsEntity();
		cmsNews.setContentCode(cmsNewsDTO.getContentCode());
		cmsNews.setSiteId(1);
		cmsNews.setCatId(cmsNewsDTO.getCatId());
		cmsNews.setContentId(Integer.parseInt(cmsNewsDTO.getContentCode()));
		cmsNews.setPublishDate(DateUtils.dateTime("yyyy-MM-dd",cmsNewsDTO.getPublishDate()));
		cmsNews.setOffDate(DateUtils.dateTime("yyyy-MM-dd",cmsNewsDTO.getOffDate()));
		cmsNews.setPriority(cmsNewsDTO.getPriority());
		cmsNews.setCreateBy(getLoginName());
		cmsNews.setCheckStatus("1");
		cmsNews.setStatus(0);
		cmsNews.setHtmlStatus("0");

		CmsNewsDataEntity cmsNewsDataEntity = new CmsNewsDataEntity();
		cmsNewsDataEntity.setContentId(Integer.parseInt(ContentNo));
		cmsNewsDataEntity.setTitle(cmsNewsDTO.getTitle());
		cmsNewsDataEntity.setFullTitle(cmsNewsDTO.getFullTitle());
		cmsNewsDataEntity.setLink(cmsNewsDTO.getLink());
		cmsNewsDataEntity.setIsNewWindow(cmsNewsDTO.getIsNewWindow());
		cmsNewsDataEntity.setColor(cmsNewsDTO.getColor());
		cmsNewsDataEntity.setNewsPath(cmsNewsDTO.getNewsPath());
		cmsNewsDataEntity.setNewsTemplate(cmsNewsDTO.getNewsTemplate());
		cmsNewsDataEntity.setDescription(cmsNewsDTO.getDescription());
		cmsNewsDataEntity.setSource(cmsNewsDTO.getSource());
		cmsNewsDataEntity.setSourceUrl(cmsNewsDTO.getSourceUrl());
		cmsNewsDataEntity.setContent(cmsNewsDTO.getContent());
		cmsNewsDataEntity.setAuthor(cmsNewsDTO.getAuthor());

		if(cmsNewsDataService.insertCmsNewsData(cmsNewsDataEntity)<0){
			return toAjax(-1);
		}
		return toAjax(cmsNewsService.insertCmsNews(cmsNews));
	}

	/**
	 * 修改内容
	 */
	@GetMapping("/edit/{contentId}")
	public String edit(@PathVariable("contentId") Integer contentId, ModelMap mmap)
	{
		CmsNewsEntity cmsNews = cmsNewsService.selectCmsNewsById(contentId);
		CmsNewsDataEntity cmsNewsDataEntity = new CmsNewsDataEntity();
		cmsNewsDataEntity.setContentId(Integer.parseInt(cmsNews.getContentCode()));
		List cmsNewsDataEntityList =  cmsNewsDataService.selectCmsNewsDataList(cmsNewsDataEntity);
		cmsNewsDataEntity = (CmsNewsDataEntity) cmsNewsDataEntityList.get(0);
		CmsNewsDTO cmsNewsDTO = new CmsNewsDTO();
		cmsNewsDTO.setContentId(cmsNews.getContentId());
		cmsNewsDTO.setSiteId(cmsNews.getSiteId());
		cmsNewsDTO.setContentCode(cmsNews.getContentCode());
		cmsNewsDTO.setCatId(cmsNews.getCatId());
		cmsNewsDTO.setCatName(cmsNews.getCatName());

		cmsNewsDTO.setPublishDate(DateUtils.parseDateToStr("yyyy-MM-dd",cmsNews.getPublishDate()));
		cmsNewsDTO.setOffDate(DateUtils.parseDateToStr("yyyy-MM-dd",cmsNews.getOffDate()));
		cmsNewsDTO.setPriority(cmsNews.getPriority());

		cmsNewsDTO.setTitle(cmsNewsDataEntity.getTitle());
		cmsNewsDTO.setFullTitle(cmsNewsDataEntity.getFullTitle());
		cmsNewsDTO.setLink(cmsNewsDataEntity.getLink());
		cmsNewsDTO.setIsNewWindow(cmsNewsDataEntity.getIsNewWindow());
		cmsNewsDTO.setColor(cmsNewsDataEntity.getColor());
		cmsNewsDTO.setNewsPath(cmsNewsDataEntity.getNewsPath());
		cmsNewsDTO.setNewsTemplate(cmsNewsDataEntity.getNewsTemplate());
		cmsNewsDTO.setDescription(cmsNewsDataEntity.getDescription());
		cmsNewsDTO.setSource(cmsNewsDataEntity.getSource());
		cmsNewsDTO.setSourceUrl(cmsNewsDataEntity.getSourceUrl());
		cmsNewsDTO.setContent(cmsNewsDataEntity.getContent());
		cmsNewsDTO.setAuthor(cmsNewsDataEntity.getAuthor());


		mmap.put("cmsNewsDTO", cmsNewsDTO);
	    return prefix + "/editNews";
	}
	
	/**
	 * 修改保存内容
	 */
	@PostMapping("/edit")
	@ResponseBody
	public ResultAjaxHash editSave(CmsNewsDTO cmsNewsDTO)
	{

		cmsNewsDTO.setCreateBy(getLoginName());


		CmsNewsEntity cmsNews = cmsNewsService.selectCmsNewsById(cmsNewsDTO.getContentId());

		cmsNews.setCatId(cmsNewsDTO.getCatId());
		cmsNews.setContentId(Integer.parseInt(cmsNews.getContentCode()));
		cmsNews.setPublishDate(DateUtils.dateTime("yyyy-MM-dd",cmsNewsDTO.getPublishDate()));
		cmsNews.setOffDate(DateUtils.dateTime("yyyy-MM-dd",cmsNewsDTO.getOffDate()));
		cmsNews.setPriority(cmsNewsDTO.getPriority());
		cmsNews.setCreateBy(getLoginName());
		cmsNews.setCheckStatus("1");
		cmsNews.setStatus(0);
		cmsNews.setHtmlStatus("0");

		CmsNewsDataEntity cmsNewsDataEntity = new CmsNewsDataEntity();
		cmsNewsDataEntity.setContentId(Integer.parseInt(cmsNews.getContentCode()));
		List cmsNewsDataEntityList =  cmsNewsDataService.selectCmsNewsDataList(cmsNewsDataEntity);

		cmsNewsDataEntity = (CmsNewsDataEntity) cmsNewsDataEntityList.get(0);

		cmsNewsDataEntity.setTitle(cmsNewsDTO.getTitle());
		cmsNewsDataEntity.setFullTitle(cmsNewsDTO.getFullTitle());
		cmsNewsDataEntity.setLink(cmsNewsDTO.getLink());
		cmsNewsDataEntity.setIsNewWindow(cmsNewsDTO.getIsNewWindow());
		cmsNewsDataEntity.setColor(cmsNewsDTO.getColor());
		cmsNewsDataEntity.setNewsPath(cmsNewsDTO.getNewsPath());
		cmsNewsDataEntity.setNewsTemplate(cmsNewsDTO.getNewsTemplate());
		cmsNewsDataEntity.setDescription(cmsNewsDTO.getDescription());
		cmsNewsDataEntity.setSource(cmsNewsDTO.getSource());
		cmsNewsDataEntity.setSourceUrl(cmsNewsDTO.getSourceUrl());
		cmsNewsDataEntity.setContent(cmsNewsDTO.getContent());
		cmsNewsDataEntity.setAuthor(cmsNewsDTO.getAuthor());

		if(cmsNewsDataService.updateCmsNewsData(cmsNewsDataEntity)<0){
			return toAjax(-1);
		}

		return toAjax(cmsNewsService.updateCmsNews(cmsNews));
	}
	
	/**
	 * 删除内容
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public ResultAjaxHash remove(String ids)
	{		
		return toAjax(cmsNewsService.deleteCmsNewsByIds(ids));
	}
	
}
